package com.siemens.industrial.web.mvc.dao.partner;

import com.siemens.industrial.model.partner.Event;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.objenesis.instantiator.sun.MagicInstantiator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Types;
import java.util.List;
import java.util.Map;

/**
 * Created by qupen on 2016/8/31.
 */
@Service
public class PartnerEventDao {
    @Resource
    JdbcTemplate jdbcTemplate;

    private static final String databaseTag = "{database}";
    private static final String getEventQuery = "SELECT * FROM `" + databaseTag + "`.`event` WHERE `event_id` = ?";
    private static final String getEventsQuery = "SELECT * FROM `" + databaseTag + "`.`event`";
    private static final String saveEventQuery = "INSERT INTO `" + databaseTag + "`.`event` (`event_id`, `event_name`, `event_type`, `event_desc`, `event_trigger`) VALUES (?,?,?,?,?)";
    private static final String updateEventQuery = "UPDATE `" + databaseTag + "`.`event` SET `event_name` = ?, `event_type` = ?, `event_desc` = ?, `event_trigger` = ? WHERE `event_id` = ?";
    private static final String disableEventQuery = "UPDATE `" + databaseTag + "`.`event` SET `enabled` = 0 WHERE `event_id` = ?";
    private static final String enableEventQuery = "UPDATE `" + databaseTag + "`.`event` SET `enabled` = 1 WHERE `event_id` = ?";
    private static final String getEventByDeviceId="SELECT a.event_id,a.event_name,a.event_type,a.event_desc,if(c.device_id is null,'false','true') isSub FROM `"+databaseTag+"`.event a left join (select * from `"+databaseTag+"`.device_event_relation b where b.device_id=?) c on a.event_id = c.event_id;";
    private static final String findEventSubscriptionListQuery = "SELECT \n" +
            "    a.event_id,\n" +
            "    a.event_name,\n" +
            "    a.event_type,\n" +
            "    a.event_desc,\n" +
            "    GROUP_CONCAT(c.device_unique_id) uniqueId\n" +
            "FROM\n" +
            "    `" + databaseTag + "`.event AS a\n" +
            "        LEFT JOIN\n" +
            "    `" + databaseTag + "`.device_event_relation b ON a.event_id = b.event_id\n" +
            "        LEFT JOIN\n" +
            "    `" + databaseTag + "`.device c ON b.device_id = c.device_id\n" +
            " GROUP BY a.event_id;";
    private static final String deleteEventQurey = "DElETE FROM`" + databaseTag + "`.`event` WHERE `event_id` = ?";

    public Event getEvent(String database, String eventId) {
        try {
            return jdbcTemplate.queryForObject(getEventQuery.replace(databaseTag, database), new Object[]{eventId}, new int[]{Types.VARCHAR}, Event.getRowMapper());
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    public List<Event> getEvents(String database) {
        return jdbcTemplate.query(getEventsQuery.replace(databaseTag, database), Event.getRowMapper());
    }

    public void updateEvent(String database, Event event) {
        jdbcTemplate.update(updateEventQuery.replace(databaseTag, database), pss -> {
                    pss.setString(1, event.getEventName());
                    pss.setString(2, event.getEventType());
                    pss.setString(3, event.getEventDesc());
                    pss.setString(4, event.getEventTrigger());
                    pss.setString(5, event.getEventId());
                }
        );
    }

    public void saveEvent(String database, Event event) {
        jdbcTemplate.update(saveEventQuery.replace(databaseTag, database), pss -> {
                    pss.setString(1, event.getEventId());
                    pss.setString(2, event.getEventName());
                    pss.setString(3, event.getEventType());
                    pss.setString(4, event.getEventDesc());
                    pss.setString(5, event.getEventTrigger());
                }
        );
    }

    public void disableEvent(String database, String eventId) {
        jdbcTemplate.update(disableEventQuery.replace(databaseTag, database), pss -> {
                    pss.setString(1, eventId);
                }
        );
    }

    public void enableEvent(String database, String eventId) {
        jdbcTemplate.update(enableEventQuery.replace(databaseTag, database), pss -> {
                    pss.setString(1, eventId);
                }
        );
    }

    /**
     * 事件订阅列表
     *
     * @param database
     * @return
     */
    public List<Map<String, Object>> eventSubList(String database) {
        return jdbcTemplate.queryForList(findEventSubscriptionListQuery.replace(databaseTag, database));
    }

    public void deleteEvent(String database, String eventId) {
        jdbcTemplate.update(deleteEventQurey.replace(databaseTag, database), eventId);
    }

    public List<Map<String,Object>> getSubEventList(String database,String deviceId){
        return jdbcTemplate.queryForList(getEventByDeviceId.replace(databaseTag,database),deviceId);
    }
}
